﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace maptool
{

    public partial class Mapedithelper : Form
    {
        public static Point mouseDownPoint;
        public static bool isSelected;
        public Mapedithelper()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show(Modtools.helpinf);
        }

        private void 省份文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Csv_openFileDialog.ShowDialog();
            

        }

        private void Csv_openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            Modtools.csvpath = Csv_openFileDialog.FileName;
            if (Modtools.csvpath == "csv_openFileDialog")
            {
                MessageBox.Show("请选择文件。");
            }
            else
            {
                string[] strArray = File.ReadAllLines(Modtools.csvpath);

                int index = 0;
                while (true)
                {
                    if (index > (strArray.Length - 1))
                    {
                        break;
                    }
                    char[] separator = new char[] { ';' };
                    string[] strArray2 = strArray[index].Split(separator);
                    object[] values = strArray2;
                    province_dataGridView.Rows.Add(values);
                    index++;
                }
            }
            for (int i = 0; i < province_dataGridView.Rows.Count; i++)
            {               
                province_dataGridView.Rows[i].Cells[6].Value = Convert.ToString(province_dataGridView.Rows[i].Cells[6].Value).Trim();
                char[] trimChars = new char[] { '#' };
                province_dataGridView.Rows[i].Cells[6].Value = Convert.ToString(province_dataGridView.Rows[i].Cells[6].Value).Trim(trimChars);
                UTF8Encoding utf8 = new UTF8Encoding();
                Byte[] encodedBytes = utf8.GetBytes(Convert.ToString(province_dataGridView.Rows[i].Cells[6].Value));
                province_dataGridView.Rows[i].Cells[6].Value = utf8.GetString(encodedBytes);
            }
        }

        private void 地图文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pic_openFileDialog.ShowDialog();
        }

        private void Pic_openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            Modtools.picpath = pic_openFileDialog.FileName;
            map_pictureBox_main.ImageLocation = Modtools.picpath;

        }

        private void Map_pictureBox_main_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isSelected = true;
            }

        }

        private bool IsMouseInPanel() =>
            ((this.map_pictureBox_main.Left < base.PointToClient(Cursor.Position).X) && ((base.PointToClient(Cursor.Position).X < (this.map_pictureBox_main.Left + this.map_pictureBox_main.Width)) && (this.map_pictureBox_main.Top < base.PointToClient(Cursor.Position).Y))) && (base.PointToClient(Cursor.Position).Y < (this.map_pictureBox_main.Top + this.map_pictureBox_main.Height));

        public static void PicMoveNotOut(PictureBox map_pictureBox_main, Point mouseDownPoint)
        {
            int right, bottom;

            int differentX = Cursor.Position.X - mouseDownPoint.X;
            int differentY = Cursor.Position.Y - mouseDownPoint.Y;

            int want2Lef = map_pictureBox_main.Left + differentX;
            int want2Top = map_pictureBox_main.Top + differentY;

            right = map_pictureBox_main.Parent.Width - (map_pictureBox_main.Right + differentX);
            bottom = map_pictureBox_main.Parent.Height - (map_pictureBox_main.Bottom + differentY);

            if (want2Lef > 0) want2Lef = map_pictureBox_main.Left;
            if (want2Top > 0) want2Top = map_pictureBox_main.Top;
            if (right > 0 && differentX < 0) want2Lef = map_pictureBox_main.Left;
            if (bottom > 0 && differentY < 0) want2Top = map_pictureBox_main.Top;

            map_pictureBox_main.Left = want2Lef;
            map_pictureBox_main.Top = want2Top;

        }

        private void Map_pictureBox_main_MouseMove(object sender, MouseEventArgs e)
        {
            if (isSelected && this.IsMouseInPanel())
            {
                this.map_pictureBox_main.Left += Cursor.Position.X - mouseDownPoint.X;
                this.map_pictureBox_main.Top += Cursor.Position.Y - mouseDownPoint.Y;
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }

        }

        private void Map_pictureBox_main_MouseUp(object sender, MouseEventArgs e)
        {
            isSelected = false;
        }

        void Map_pictureBox_main_MouseWheel(object sender, MouseEventArgs e)
        {
            int i = e.Delta * SystemInformation.MouseWheelScrollLines / 5;

            map_pictureBox_main.Width = map_pictureBox_main.Width + i;//增加picturebox的宽度
            map_pictureBox_main.Height = map_pictureBox_main.Height + i;
            map_pictureBox_main.Left = map_pictureBox_main.Left - i / 2;//使picturebox的中心位于窗体的中心
            map_pictureBox_main.Top = map_pictureBox_main.Top - i / 2;//进而缩放时图片也位于窗体的中心
        }

        [DllImport("gdi32.dll")]
        private static extern int GetPixel(IntPtr hdc, Point p);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        private void map_pictureBox_main_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point p = new Point(Control.MousePosition.X, Control.MousePosition.Y);
            int pixel = GetPixel(GetDC(new IntPtr(0)), p);
            Modtools.R = pixel & 0xff;
            Modtools.G = (pixel & 0xff00) / 0x100;
            Modtools.B = (pixel & 0xff_0000) / 0x1_0000;
            Modtools.red = Convert.ToString(Modtools.R);
            Modtools.green = Convert.ToString(Modtools.G);
            Modtools.blue = Convert.ToString(Modtools.B);
            infrefreshbutton_Click(sender, e);
            颜色ToolStripMenuItem_Click(sender, e);
        }

        private void colortimer_Tick(object sender, EventArgs e)
        {
            inflabelpanel.BackColor = Color.FromArgb(255, Modtools.R, Modtools.G, Modtools.B);
        }

        private void infrefreshbutton_Click(object sender, EventArgs e)
        {
            idtextBox.Text = Modtools.ID;
            nametextBox.Text = Modtools.name;
            RtextBox.Text = Modtools.red;
            GtextBox.Text = Modtools.green;
            BtextBox.Text = Modtools.blue;
            remarktextBox.Text = Modtools.remark;
        }        

        private void 序号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int count = province_dataGridView.Rows.Count;
            int num2 = 0;
            while (true)
            {
                if (num2 < count)
                {
                    if ((Convert.ToString(this.province_dataGridView.Rows[num2].Cells[0].Value) != idtextBox.Text) && (Convert.ToString(this.province_dataGridView.Rows[num2].Cells[0].Value) != ("#" + idtextBox.Text)))
                    {
                        num2++;
                        continue;
                    }
                    province_dataGridView.CurrentCell = province_dataGridView[0, num2];
                    province_dataGridView_Click(sender, e);
                }
                return;
            }
        }

        private void province_dataGridView_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[this.province_dataGridView.CurrentCell.ColumnIndex].Value) == "")
            {
                Modtools.ID = "";
                Modtools.R = 255;
                Modtools.G = 255;
                Modtools.B = 255;
                Modtools.red = "";
                Modtools.green = "";
                Modtools.blue = "";
                Modtools.name = "";
                Modtools.remark = "";
            }
            else
            {
                Modtools.R = Convert.ToInt32(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[1].Value);
                Modtools.G = Convert.ToInt32(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[2].Value);
                Modtools.B = Convert.ToInt32(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[3].Value);
                Modtools.red = Convert.ToString(Modtools.R);
                Modtools.green = Convert.ToString(Modtools.G);
                Modtools.blue = Convert.ToString(Modtools.B);
                Modtools.ID = Convert.ToString(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[0].Value);
                Modtools.name = Convert.ToString(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[4].Value);
                Modtools.remark = Convert.ToString(this.province_dataGridView.Rows[this.province_dataGridView.CurrentRow.Index].Cells[6].Value);
            }
            infrefreshbutton_Click(sender, e);
        }

        private void 名称ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int count = this.province_dataGridView.Rows.Count;
            string str = nametextBox.Text.ToUpper();
            int namecount = Modtools.namecount;
            while (true)
            {
                if (namecount < count)
                {
                    int index = Convert.ToString(this.province_dataGridView.Rows[namecount].Cells[4].Value).ToUpper().IndexOf(str);
                    if (index == -1)
                    {
                        if ((namecount == (count - 1)) && (index == -1))
                        {
                            MessageBox.Show("未找到！再次点击从头开始寻找。", "错误");
                            namecount = 0;
                        }
                        namecount++;
                        continue;
                    }
                    province_dataGridView.CurrentCell = this.province_dataGridView[4, namecount];
                    province_dataGridView_Click(sender,e);
                    Modtools.namecount = (Modtools.namecount >= count) ? 0 : (namecount + 1);
                }
                return;
            }

        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int count = this.province_dataGridView.Rows.Count;
            int num2 = 0;
            while (true)
            {
                if (num2 < count)
                {
                    int num3 = 0;
                    if (((Convert.ToString(this.province_dataGridView.Rows[num2].Cells[1].Value) != this.RtextBox.Text) || (Convert.ToString(this.province_dataGridView.Rows[num2].Cells[2].Value) != this.GtextBox.Text)) || (Convert.ToString(this.province_dataGridView.Rows[num2].Cells[3].Value) != this.BtextBox.Text))
                    {
                        if ((num2 == (count - 1)) && (num3 == 0))
                        {
                            MessageBox.Show("未找到！再次点击从头开始寻找。", "错误");
                            num2 = 0;
                        }
                        num2++;
                        continue;
                    }
                    this.province_dataGridView.CurrentCell = this.province_dataGridView[0, num2];
                    province_dataGridView_Click(sender, e);
                    num3 = 1;
                }
                return;
            }

        }

        private void infwritebutton_Click(object sender, EventArgs e)
        {
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[0].Value = idtextBox.Text;
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[1].Value = RtextBox.Text;
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[2].Value = GtextBox.Text;
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[3].Value = BtextBox.Text;
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[4].Value = nametextBox.Text;
            province_dataGridView.Rows[province_dataGridView.CurrentRow.Index].Cells[6].Value = remarktextBox.Text;
            province_dataGridView_Click(sender, e);
        }

        private void 备注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int count = province_dataGridView.Rows.Count;
            string str = remarktextBox.Text.ToUpper();
            int namecount = 0;
            while (true)
            {
                if (namecount < count)
                {
                    int index = Convert.ToString(province_dataGridView.Rows[namecount].Cells[6].Value).ToUpper().IndexOf(str);
                    if (index == -1)
                    {
                        if ((namecount == (count - 1)) && (index == -1))
                        {
                            MessageBox.Show("未找到！再次点击从头开始寻找。", "错误");
                            namecount = 0;
                        }
                        namecount++;
                        continue;
                    }
                    province_dataGridView.CurrentCell = this.province_dataGridView[6, namecount];
                    province_dataGridView_Click(sender, e);
                    Modtools.namecount = (Modtools.namecount >= count) ? 0 : (namecount + 1);
                }
                return;
            }
        }

        private void 生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] textArray1 = new string[0x11];
            textArray1[0] = "b_";
            textArray1[1] = this.nametextBox.Text;
            textArray1[2] = " = { #";
            textArray1[3] = this.remarktextBox.Text;
            textArray1[4] = "\r\n    province = ";
            textArray1[5] = this.idtextBox.Text;
            textArray1[6] = "\r\n    \r\n    color = {";
            textArray1[7] = this.RtextBox.Text;
            textArray1[8] = " ";
            textArray1[9] = this.GtextBox.Text;
            textArray1[10] = " ";
            textArray1[11] = this.BtextBox.Text;
            textArray1[12] = "}\r\n    color2 = {255 255 255}\r\n    \r\n}\r\n\r\n ";
            textArray1[13] = this.nametextBox.Text;
            textArray1[14] = ":0 \"";
            textArray1[15] = this.remarktextBox.Text;
            textArray1[0x10] = "\"";
            this.resultTextBox.Text = string.Concat(textArray1);

        }

        private void RtextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.RtextBox.Text != "")
            {
                if (Regex.IsMatch(this.RtextBox.Text, "^[0-9]*$"))
                {
                    int R = Convert.ToInt32(this.RtextBox.Text);
                    if ((R >= 0) && (R <= 0xff))
                    {
                        
                    }
                    else
                    {
                        MessageBox.Show("请输入0-255的整数!", "数据错误");
                        this.RtextBox.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("请输入0-255的整数!", "数据错误");
                    this.RtextBox.Text = "";
                }
            }

        }

        private void GtextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.GtextBox.Text != "")
            {
                if (Regex.IsMatch(this.GtextBox.Text, "^[0-9]*$"))
                {
                    int G = Convert.ToInt32(this.GtextBox.Text);
                    if ((G >= 0) && (G <= 0xff))
                    {

                    }
                    else
                    {
                        MessageBox.Show("请输入0-255的整数!", "数据错误");
                        this.GtextBox.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("请输入0-255的整数!", "数据错误");
                    this.GtextBox.Text = "";
                }
            }
        }

        private void BtextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.BtextBox.Text != "")
            {
                if (Regex.IsMatch(this.BtextBox.Text, "^[0-9]*$"))
                {
                    int B = Convert.ToInt32(this.BtextBox.Text);
                    if ((B >= 0) && (B <= 0xff))
                    {

                    }
                    else
                    {
                        MessageBox.Show("请输入0-255的整数!", "数据错误");
                        this.BtextBox.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("请输入0-255的整数!", "数据错误");
                    this.BtextBox.Text = "";
                }
            }
        }

        private void 省份文件ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            csv_saveFileDialog.ShowDialog();
        }

        private void csv_saveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string fileName = this.csv_saveFileDialog.FileName;
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            for (int i = 0; i < (this.province_dataGridView.Rows.Count - 1); i++)
            {
                string[] textArray1 = new string[] 
                { $"{this.province_dataGridView.Rows[i].Cells[0].Value};", 
                  $"{this.province_dataGridView.Rows[i].Cells[1].Value};", 
                  $"{this.province_dataGridView.Rows[i].Cells[2].Value};", 
                  $"{this.province_dataGridView.Rows[i].Cells[3].Value};", 
                  $"{this.province_dataGridView.Rows[i].Cells[4].Value};", 
                  $"{this.province_dataGridView.Rows[i].Cells[5].Value};", 
                  $" #{this.province_dataGridView.Rows[i].Cells[6].Value}\r\n" 
                };
            File.AppendAllText(fileName, string.Concat(textArray1));
            }
        }

        private void province_saveFileDialog_FileOk(object sender, CancelEventArgs e)
        {            
            string fileName = this.province_saveFileDialog.FileName;
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            for (int i = 1; i < (this.province_dataGridView.Rows.Count - 1); i++)
            {
                string[] textArray1 = new string[] 
                { $"b_{this.province_dataGridView.Rows[i].Cells[4].Value} = {{ #{this.province_dataGridView.Rows[i].Cells[6].Value}\r\n" +
                  $"\r\n" +
                  $"    province = {this.province_dataGridView.Rows[i].Cells[0].Value}\r\n" +
                  $"\r\n" +
                  $"    color = {{{this.province_dataGridView.Rows[i].Cells[1].Value} {this.province_dataGridView.Rows[i].Cells[2].Value} {this.province_dataGridView.Rows[i].Cells[3].Value}}}\r\n" +
                  $"    color2 = {{255 255 255}}\r\n" +
                  $"\r\n" +
                  $"}}\r\n" +
                  $"\r\n"
                };
                File.AppendAllText(fileName, string.Concat(textArray1));
            }

        }

        private void 省份代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            province_saveFileDialog.ShowDialog();
        }

        private void 本地化代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            localization_saveFileDialog.ShowDialog();
        }

        private void localization_saveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string fileName = this.localization_saveFileDialog.FileName;
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.AppendAllText(fileName, "l_simp_chinese:");
            for (int i = 0; i < (this.province_dataGridView.Rows.Count - 1); i++)
            {
                File.AppendAllText(fileName, $" {this.province_dataGridView.Rows[i].Cells[4].Value}:0 \"{ this.province_dataGridView.Rows[i].Cells[6].Value}\"");
            }

        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Modtools.helpinf);
        }
    }
}
